package cn.edu.dutir.indri;

import lemurproject.lemur.ArrayAccumulator;
import lemurproject.lemur.Index;
import lemurproject.lemur.IndexManager;
import lemurproject.lemur.IndexedReal;
import lemurproject.lemur.RetMethodManager;
import lemurproject.lemur.RetrievalMethod;

public class LemurRunQuery {

	public static void main(String[] args) {
		// we assume the index path is the first argument and the query is
		// second
		String indexPath = "d:/CWT200g_url2docno_index";
		String query = "asos";
		try {
			// open the index
			Index theIndex = IndexManager.openIndex(indexPath);
			// create an accumulator for the index
			ArrayAccumulator scoreAccum = new ArrayAccumulator(theIndex
					.docCount());
			// create our retrieval method using OKAPI
			RetrievalMethod retMethod = RetMethodManager.createModel(theIndex,
					scoreAccum, "okapi");
			// run our query
			IndexedReal[] results = retMethod.runQuery(query);
			int totalNumResults = results.length;
			// for each result, print out the rank, document ID and the score
			for (int i = 0; i < totalNumResults; i++) {
				// get the document ID
				int thisResultDocID = results[i].ind;
				// get the score
				double thisResultScore = results[i].val;
				// print the rank, document ID and the score
				System.out.println((i + 1) + "\t" + thisResultDocID + "\t"
						+ thisResultScore);
			}
			// when theIndex goes out of scope, it will be closed
			// and disposed of.
		} catch (Exception e) {
			System.out.println("Exception was thrown: " + e.getMessage());
		}
	}
}
